<?php

namespace App\Http\Requests;
use Illuminate\Validation\Rule;

/**
 * 表单验证
 * Class CodeValueSaveRequest
 * @package App\Http\Requests
 */
class CodeValueSaveRequest extends BaseRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->get('id', 0);
        $code = $this->get('code', 0);
        $rules = [
            'name' => [
                'required',
                'between:2,50'
            ],
            'value' => [
                'required',
                'integer',
                'max:255',
                Rule::unique('code_values', 'value')->where('deleted_at', 0)->where('code', $code)->ignore($id)
            ], 'desc' => [
                'max:250'
            ],
        ];
        if (!$id) {
            $rules['code'] = [
                'required'
            ];
        }
        return $rules;
    }

    public function messages()
    {
        return [
            'desc.max' => '备注不超过:max 个字符',
            'name.required' => '请输入名称',
            'name.between' => '名称字符长度:min-:max',
            'value.required' => '请输入值',
            'value.integer' => '值编号必须是整数',
            'value.unique' => '值编号必须唯一',
            'value.max' => '值编号必须小于等于:max',
            'code.required' => '代码编号不存在'
        ];
    }
}
